import socket

# 配置参数
LOCAL_HOST = '127.0.0.1'  # 监听本地接口
PORT = 3000  # 监听的端口
BUFFER_SIZE = 4096
REMOTE_IP = '127.0.0.1'  # 目标机器的IP（在这种情况下是同一台机器，用于测试）
REMOTE_PORT = 5000  # 转发数据到该端口

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((LOCAL_HOST, PORT))
print("Listening on port: ", PORT)

while True:
    data, addr = s.recvfrom(BUFFER_SIZE)

    # 打印接收到的数据并将其转发到远程端口
    print('Received from ', addr[0], ':', addr[1], ' -> Data: ', data.decode())

    remote = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    remote.sendto(data, (REMOTE_IP, REMOTE_PORT))